package com.crm.crm.service.impl;

import com.alibaba.fastjson.JSON;
import com.crm.crm.mapper.CrmSelectMapper;
import com.crm.crm.service.CrmSelectService;
import com.crm.entity.Partner;
import com.crm.model.Telephone;
import com.crm.model.Workers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CrmSelectServiceImpl implements CrmSelectService {
    @Autowired
    private CrmSelectMapper mapper;

    @Override
    public PageInfo<Partner> findAsset(String userId, Integer partnerType, String bizType,
                                       String[] cities, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findAsset(partnerType, bizType, cities);
        try {
            for (Partner partner : partnerList) {
                if (!userId.equals(partner.getCreateUserId())) {
                    partner.setName((partner.getName().subSequence(0, 1)) + "**");
                    if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                        partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                    }
                    if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                        partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                    }
                    if (partner.getTelephoneList().size() > 0) {
                        List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                                Telephone.class);
                        for (int i = 0; i < t.size(); i++) {
                            t.get(i).setTelephones("******");
                        }
                        partner.setTelephoneList(t);
                    }
                    partner.setEmail("******@**.com");
                    partner.setCompany("********");
                }
            }
        } catch (Exception e) {

        }


        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findByPost(String userId, Integer partnerType, String post, String[] cities, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findByPost(partnerType, post, cities);
        try {
            for (Partner partner : partnerList) {
                if (!userId.equals(partner.getCreateUserId())) {
                    partner.setName((partner.getName().subSequence(0, 1)) + "**");
                    if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                        partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                    }
                    if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                        partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                    }
                    if (partner.getTelephoneList().size() > 0) {
                        List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                                Telephone.class);
                        for (int i = 0; i < t.size(); i++) {
                            t.get(i).setTelephones("******");
                        }
                        partner.setTelephoneList(t);
                    }
                    partner.setEmail("******@**.com");
                    partner.setCompany("********");

                }
            }
        } catch (Exception e) {

        }

        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findByNameTel(Integer partnerType, String create_user_id, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findByNameTel(partnerType, create_user_id);
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findByNameTelHide(String userId, String roleId, Integer partnerType, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);

        List<Partner> partnerList = mapper.findByNameTelHide(roleId, partnerType);
        try {
            for (Partner partner : partnerList
            ) {
                if (!userId.equals(partner.getCreateUserId())) {
                    partner.setName((partner.getName().subSequence(0, 1)) + "**");
                    if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                        partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                    }
                    if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                        partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                    }
                    if (partner.getTelephoneList().size() > 0) {
                        List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                                Telephone.class);
                        for (int i = 0; i < t.size(); i++) {
                            t.get(i).setTelephones("******");
                        }
                        partner.setTelephoneList(t);
                    }
                    partner.setEmail("******@**.com");
                    partner.setCompany("********");
                }
            }
        } catch (StringIndexOutOfBoundsException e) {
        }
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findByNameTelHide1(String roleId, Integer partnerType, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findByNameTelHide(roleId, partnerType);
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findByBlurry01(Integer partnerType, String value, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.findByBlurry01(partnerType, value));
        return pageInfo;
    }

    @Override
    public PageInfo<Partner> findByBlurry02(Integer partnerType, String value, String createId, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.findByBlurry02(partnerType, value, createId));
        return pageInfo;
    }

    @Override
    public PageInfo<Partner> findExitInfo(String createId, String userId, String company, Integer partnerType, String orgType, String orgRemark, String usage, String typeId, String typeName, String[] cities, Integer abilityMin, Integer abilityMax, int pageIndex, int pageSize) {

        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findExitInfo(createId, company, partnerType, orgType, orgRemark, usage, typeId, typeName, cities, abilityMin, abilityMax);
        try {
            for (Partner partner : partnerList) {
                if (!userId.equals(partner.getCreateUserId())) {
                    partner.setName((partner.getName().subSequence(0, 1)) + "**");
                    if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                        partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                    }
                    if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                        partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                    }
                    if (partner.getTelephoneList().size() > 0) {
                        List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                                Telephone.class);
                        for (int i = 0; i < t.size(); i++) {
                            t.get(i).setTelephones("******");
                        }
                        partner.setTelephoneList(t);
                    }
                    partner.setEmail("******@**.com");
                    partner.setCompany("********");
                }else {
                    partner.setSelf(1);
                }
            }
        } catch (Exception e) {

        }

        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findExitInfo1(String depId, String createId, String company, Integer partnerType, String orgType, String orgRemark, String usage, String typeId, String typeName, String[] cities, Integer abilityMin, Integer abilityMax, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findExitInfoSys(depId, createId, company, partnerType, orgType, orgRemark, usage, typeId, typeName, cities, abilityMin, abilityMax);
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findPeerInfo(String createId, String userId, String company, Integer partnerType, String orgType,
                                          String bizListId, String coopListId
            , String[] cities, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findPeerInfo(createId, company, partnerType, orgType, bizListId, coopListId, cities);
        try {
            for (Partner partner : partnerList) {
                if (!userId.equals(partner.getCreateUserId())) {
                    partner.setName((partner.getName().subSequence(0, 1)) + "**");
                    if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                        partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                    }
                    if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                        partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                    }
                    if (partner.getTelephoneList().size() > 0) {
                        List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                                Telephone.class);
                        for (int i = 0; i < t.size(); i++) {
                            t.get(i).setTelephones("******");
                        }
                        partner.setTelephoneList(t);
                    }
                    partner.setEmail("******@**.com");
                    partner.setCompany("********");
                }else {
                    partner.setSelf(1);
                }
            }
        } catch (Exception e) {

        }

        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findPeerInfo1(String depId, String createId, String company, Integer partnerType, String orgType,
                                           String bizListId, String coopListId
            , String[] cities, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findPeerInfoSys(depId, createId, company, partnerType, orgType, bizListId, coopListId, cities);
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findJusticeInfo(String createId, String userId, Integer partnerType, String company,
                                             String post, String[] cities,
                                             int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findJusticeInfo(createId, partnerType, company, post, cities);
        try {
            for (Partner partner : partnerList) {
                if (!userId.equals(partner.getCreateUserId())) {
                    partner.setName((partner.getName().subSequence(0, 1)) + "**");
                    if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                        partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                    }
                    if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                        partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                    }
                    if (partner.getTelephoneList().size() > 0) {
                        List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                                Telephone.class);
                        for (int i = 0; i < t.size(); i++) {
                            t.get(i).setTelephones("******");
                        }
                        partner.setTelephoneList(t);
                    }
                    partner.setEmail("******@**.com");
                    partner.setCompany("********");
                }
            }
        } catch (Exception e) {

        }

        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findJusticeInfo1(String depId, String createId, Integer partnerType, String company, String post, String[] cities, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findJusticeInfoSys(depId, createId, partnerType, company, post, cities);
        return new PageInfo<>(partnerList);
    }


    @Override
    public PageInfo<Partner> findOrgInfo(String createId, String userId, Integer partnerType, String company, String post, String[] cities, int pageIndex, int pageSize) {

        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findOrgInfo(createId, partnerType, company, post, cities);
        try {
            for (Partner partner : partnerList) {
                if (!userId.equals(partner.getCreateUserId())) {
                    partner.setName((partner.getName().subSequence(0, 1)) + "**");
                    if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                        partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                    }
                    if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                        partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                    }
                    if (partner.getTelephoneList().size() > 0) {
                        List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                                Telephone.class);
                        for (int i = 0; i < t.size(); i++) {
                            t.get(i).setTelephones("******");
                        }
                        partner.setTelephoneList(t);
                    }
                    partner.setEmail("******@**.com");
                    partner.setCompany("********");
                }else {
                    partner.setSelf(1);
                }
            }
        } catch (Exception e) {

        }

        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findOrgInfo1(String depId, String createId, Integer partnerType, String company, String post, String[] cities, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Partner> partnerList = mapper.findOrgInfoSys(depId, createId, partnerType, company, post, cities);
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findById(String partner_id, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.findById(partner_id));
        return pageInfo;
    }

    @Override
    public Partner matchNumber(String telephone, String create_user_id) {
        return mapper.matchNumber(telephone, create_user_id);
    }

    @Override
    public Integer countCreateId(String createId) {
        return mapper.countCreateId(createId);
    }

    @Override
    public List<Workers> workers() {

        return mapper.workers();
    }

    @Override
    public List<String> depName() {
        return mapper.depName();
    }

    @Override
    public PageInfo<Partner> personal(int partnerType, String userId, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.personal(partnerType, userId));
        return pageInfo;
    }

    @Override
    public Partner telephoneNumber(String telephone, String create_user_id) {
        return mapper.telephoneNumber(telephone, create_user_id);
    }

    @Override
    public Partner emailNumber(String email, String create_user_id) {
        return mapper.emailNumber(email, create_user_id);
    }

    @Override
    public Partner weixinNumber(String weixin, String create_user_id) {
        return mapper.weixinNumber(weixin, create_user_id);
    }


}
